#!perl

use strict;

my ($fn,$h8,$j1,$sc,$py,$ut,$t9,$ti,$au,$sc_present,$au_present); 

$sc_present=0;
$au_present=0;

print "FN,H8,J1,SC,PY,UT,T9,TI,AU\n";

while(<>) { 
$fn = $1 if /^FN\s+(.+)/;
$h8 = $1 if /^H8\s+(.+)/;
$j1 = $1 if /^J1\s+(.+)/;
if (/^SC\s+[A-Z]{1,2}\s+([A-Z]{1,2}).+/) {
	if ($sc_present==1) {
		$sc = $sc.";".$1
	}
	if ($sc_present==0) {
		$sc = $1
	}
	$sc_present=1;
}
else {
	$sc_present=0;
}
$py = $1 if /^PY\s+(\d+)/;
$ut = $1 if /^UT\s+(.+)/;
$t9 = $1 if /^T9\s+(\d+)/; 
$ti = $1 if /^TI\s+(.+)/;
$ti =~ tr/"//d;
$ti =~ tr/\\//d;
if (/^AU\s+(.+)/) {
	if ($au_present==1) {
		$au = $au.";".$1
	}
	if ($au_present==0) {
		$au = $1
	}
	$au_present=1;
}
else {
	$au_present=0;
}
print "$fn,$h8,$j1,$sc,$py,$ut,$t9,\"$ti\",\"$au\"\n" if (/^EX/);
}

